import os
import json

def process_tags(data_item):
    """处理单条数据中的 tag 字段"""
    title_tags = data_item.get("title", "").split()
    tag_tags = data_item.get("tag", "").split('|')

    # 合并、去重（保留顺序）
    all_tags = title_tags + tag_tags
    seen = set()
    deduped_tags = []
    for tag in all_tags:
        if tag not in seen:
            seen.add(tag)
            deduped_tags.append(tag)

    # 更新 tag 字段
    data_item["tag"] = ",".join(deduped_tags)
    return data_item

def process_file(input_path, output_path):
    """处理单个 JSON 文件"""
    try:
        with open(input_path, 'r', encoding='utf-8') as f:
            data = json.load(f)

        if isinstance(data, list):
            processed_data = [process_tags(item) for item in data]
        else:
            processed_data = [process_tags(data)]

        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(processed_data, f, ensure_ascii=False, indent=2)
        print(f"✅ 已处理: {input_path} → {output_path}")

    except Exception as e:
        print(f"❌ 处理失败: {input_path}，错误: {e}")

def process_all_json_files(input_dir, output_dir):
    """遍历处理整个文件夹下的 JSON 文件"""
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    for filename in os.listdir(input_dir):
        if filename.endswith(".json"):
            input_path = os.path.join(input_dir, filename)
            output_path = os.path.join(output_dir, filename)
            process_file(input_path, output_path)

# 👉 修改为你实际的文件夹路径
input_directory = "jsons_GZ_new"   # 输入目录
output_directory = "jsons_GZ_new_Pro" # 输出目录

process_all_json_files(input_directory, output_directory)
